
import { stringify } from 'query-string';
import 'whatwg-fetch';
let wx = window.wx;
const url = window.location.href.split('#')[0];
const linkUrl = __PRO__ ? 'http://funs.4000916916.com/activityConfig/index.html#/memberSelf'
: 'http://wx-test.by-health.com/activityConfig/index.html#/memberSelf';
export function config() {
  // appId: wx2f995336548675b4生产      appId: wxb425b33623e260d4测试
  const data = {
    url,
    appId: __PRO__?'wx2f995336548675b4':'wxb425b33623e260d4',
  }
  fetch("/clerkconfig/clerkconfig/JsSdkAuth.do", {
    method: 'post',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    credentials: 'same-origin',
    body: stringify(data),
  })
  .then((response) => {
    response.json()
    .then((datas) => {
      if(datas.status === '0') {
        if (!wx) wx = window.wx;
        wx.config({
          debug: false,
          appId: datas.body.appId,
          timestamp: datas.body.timestamp,
          nonceStr: datas.body.nonceStr,
          signature: datas.body.signature,
          jsApiList: [
            'showMenuItems',
            'hideMenuItems',
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
          ],
        });
        wx.ready(() => {
          wx.hideMenuItems({
            menuList: [
                 'menuItem:share:appMessage',
                 'menuItem:share:timeline',
                 'menuItem:share:qq',
                 'menuItem:share:weiboApp',
                 'menuItem:share:facebook',
                 'menuItem:share:QZone',
                 'menuItem:favorite',
                 'menuItem:openWithQQBrowser',
                 'menuItem:openWithSafari',
              ],
          })
        });
      } else {
        alert(datas.errorMsg);
      }
    })
    .catch((datas) => {
    })
  })
}

export function canShareConfig(phone, headimg) {
  // appId: wx2f995336548675b4生产      appId: wxb425b33623e260d4测试
  const data = {
    url,
    appId: __PRO__?'wx2f995336548675b4':'wxb425b33623e260d4',
  }
  fetch("/clerkconfig/clerkconfig/JsSdkAuth.do", {
    method: 'post',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    credentials: 'same-origin',
    body: stringify(data),
  })
  .then((response) => {
    response.json()
    .then((datas) => {
      if(datas.status === '0') {
        if (!wx) wx = window.wx;
        wx.config({
          debug: false,
          appId: datas.body.appId,
          timestamp: datas.body.timestamp,
          nonceStr: datas.body.nonceStr,
          signature: datas.body.signature,
          jsApiList: [
            'showMenuItems',
            'hideMenuItems',
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
            'onMenuShareQQ',
            'onMenuShareWeibo',
            'onMenuShareQZone',
          ],
        });
        wx.ready(() => {
            wx.hideMenuItems({
              menuList: [
                   'menuItem:share:qq',
                   'menuItem:share:weiboApp',
                   'menuItem:share:facebook',
                   'menuItem:share:QZone',
                   'menuItem:favorite',
                   'menuItem:openWithQQBrowser',
                   'menuItem:openWithSafari',
                ],
            });
            wx.showMenuItems({
                menuList: [
                  'menuItem:share:appMessage',
                  'menuItem:share:timeline',
                ]
            });
            // http://funs.4000916916.com/activityConfig/index.html?/memberSelf?
            wx.onMenuShareTimeline({
              title: '会员自助', // 分享标题
              desc: '您可自行积分或参与活动，更多丰富礼品等着你哦。', // 分享描述
              link: `${linkUrl}?phone=${phone}&headimg=${headimg}`, // 分享链接
              imgUrl: 'http://cdn-yyj.4000916916.com/yygjapp/2016share.png', // 分享图标
              type: '', // 分享类型,music、video或link，不填默认为link
              dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
              success: function () {
                  // 用户确认分享后执行的回调函数
              },
              cancel: function () {
                  // 用户取消分享后执行的回调函数
              }
            });
            wx.onMenuShareAppMessage({
              title: '会员自助', // 分享标题
              desc: '您可自行积分或参与活动，更多丰富礼品等着你哦。', // 分享描述
              link: `${linkUrl}?phone=${phone}&headimg=${headimg}`, // 分享链接
              imgUrl: 'http://cdn-yyj.4000916916.com/yygjapp/2016share.png', // 分享图标
              type: '', // 分享类型,music、video或link，不填默认为link
              dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
              success: function () {
                  // 用户确认分享后执行的回调函数
              },
              cancel: function () {
                  // 用户取消分享后执行的回调函数
              }
           });
        });
      } else {
        alert(datas.errorMsg);
      }
    })
    .catch((datas) => {
    })
  })
}
export function close() {
  try {
    wx.closeWindow();
    return true;
  } catch (e) {
    return false;
  }
}

export default {};
